package com.example.springbootredis.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.example.springbootredis.entity.User;
import com.example.springbootredis.util.BaseResp;
import com.example.springbootredis.util.TestDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Slf4j
@Api("redis【list】")
@RestController
@RequestMapping("/redis/list")
public class RedisListController {

    @Autowired
    RedisTemplate redisTemplate;

    //-----------------redis list start-------------
    String testListKey = "test_list";

    @ApiOperation("初始化数据")
    @RequestMapping(value = "/initData",method = RequestMethod.GET)
    public BaseResp list_initData(){
        log.info("list_initData start");
        redisTemplate.opsForList().rightPushAll(testListKey,TestDataUtil.getUsers().toArray());
        log.info("list_initData end");
        return BaseResp.buildSuccess();
    }

    @ApiOperation("根据下标 取数据")
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public BaseResp list_index(@RequestParam("index")Integer index){
        Object user = redisTemplate.opsForList().index(testListKey,index);
        return BaseResp.buildDataSuccess(user);
    }

    @ApiOperation("根据下标 取数据")
    @RequestMapping(value = "/list/range",method = RequestMethod.GET)
    public BaseResp range(@RequestParam("start")Integer start,@RequestParam("end")Integer end){
        Object user = redisTemplate.opsForList().range(testListKey,start,end);
        return BaseResp.buildDataSuccess(user);
    }
    String testListPopKey = "test_list_pop";

    @ApiOperation("list-拉取")
    @RequestMapping(value = "/pop",method = RequestMethod.GET)
    public BaseResp list_pop(){
        Object popResp = redisTemplate.opsForList().leftPop(testListPopKey, Duration.ofMinutes(1));
        log.info("popResp:{}",popResp);
        return BaseResp.buildDataSuccess(popResp);
    }
    @ApiOperation("list-推送")
    @RequestMapping(value = "/pop/leftPush",method = RequestMethod.GET)
    public BaseResp list_pop(@RequestParam("pushVal")String pushVal){
        Long resp = redisTemplate.opsForList().leftPush(testListPopKey,pushVal);
        return BaseResp.buildDataSuccess(resp);
    }
    //-----------------redis list end-------------

}
